﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace SLPropertyGrid
{
	public sealed class CategoryItem : INotifyPropertyChanged
	{
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name"></param>
		public CategoryItem(string name)
		{
			if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
			Name = name;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="category"></param>
		public CategoryItem(CategoryAttribute category)
			: this(category.Category)
		{
			this.Attribute = category;
		}

		#endregion

		public string Name { get; private set; }
		public Attribute Attribute { get; private set; }


		public bool IsExpanded
		{
			get { return _isExpanded; }
			set
			{
				if (_isExpanded == value) return;
				_isExpanded = value;
				OnPropertyChanged("IsExpanded");
			}
		} private bool _isExpanded = true;

		private ObservableCollection<PropertyItem> properties = new ObservableCollection<PropertyItem>();
		public IEnumerable<PropertyItem> Properties
		{
			get { return properties; }
		}

		public PropertyItem this[string propertyName]
		{
			get { return this.Properties.Where(entry => entry.Name == propertyName).FirstOrDefault(); }
		}

		public void AddProperty(PropertyItem property)
		{
			this.properties.Add(property);
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(string propertyName)
		{
			if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion

	}
}
